<?php

namespace App\Models;

use App\Libs\DataFormat;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Project extends Model
{

    use DataFormat;

    protected $table = 'project';
    protected $dates = [''];

    public static  $projectStatus = ['1'=>'未开始', '2'=>'招募中', '3'=>'已满员', '4'=>'进行中', '5'=>'已结束'];

    public static  $formatKey = ['id'=>'int', 'name'=>'string','list_photo'=>'image_url', 'photo'=>'json_decode', 'volunteer_security'=>'string', 'identifier'=>'string', 'province_id'=>'int', 'province_name'=>'string', 'city_id'=>'int', 'city_name'=>'string', 'county_id'=>'int', 'county_name'=>'string', 'addr'=>'string', 'content'=>'string', 'join_end'=>'string', 'begin'=>'string', 'end'=>'string', 'people_count'=>'int', 'join_people_count'=>'int', 'lng'=>'string', 'lat'=>'string', 'reward_time'=>'string', 'created_at'=>'string' ,'contact_phone_public'=>'int','contact_name'=>'string','contact_phone'=>'string', 'custom_config'=>'json_decode', 'custom_payment_config'=>'json_decode', ];

    protected $casts = [
        'custom_config' => 'array',
        'custom_payment_config' => 'array',
    ];

    public function org()
    {
        return $this->hasOne('App\Models\Org', 'id', 'org_id');
    }

    public function getAllInfo()
    {
        $data = Project::format($this);
        $data['project_status'] = Project::projectStatus($this);
        $data['distance'] = $this->getDistance();
        $data['team'] = Team::format($this->team);

        $category = $this->category;
        $data['category'] = [];
        foreach($category as $value)
        {
            $data['category'][] = ProjectCategory::format($value);
        }

        $serviceObj = $this->serviceObj;
        $data['service_object'] = [];
        foreach($serviceObj as $value)
        {
            $data['service_object'][] = ProjectServiceObj::format($value);
        }

        foreach($data['photo'] as $key=>$value)
        {
            $data['photo'][$key] = image_url($value);
        }

        return $data;
    }
    public function category()
    {
        return $this->hasMany('App\Models\ProjectCategory', 'project_id', 'id');
    }

    public function serviceObj()
    {
        return $this->hasMany('App\Models\ProjectServiceObj', 'project_id', 'id');
    }

    public function team()
    {
        return $this->hasOne('App\Models\Team', 'id', 'team_id');
    }


    public static function projectStatus($project)
    {

        if(time() > strtotime($project->end))
        {
            return 5;
        }elseif(time() > strtotime($project->begin))
        {
            return 4;
        }else
        {
            return $project->join_people_count>=$project->people_count? 3:2;
        }
        //if(time() < strtotime($project->join_begin))
        //{
        //    return 1;
        //}else
        //if(date('Y-m-d') <= $project->join_end)
        //{
        //    return $project->join_people_count>=$project->people_count? 3:2;
        //}elseif(time() < strtotime($project->begin))
        //{
        //    return 2;
        //}elseif(time() < strtotime($project->end))
        //{
        //    return 4;
        //}else
        //{
        //    return 5;
        //}
    }



    public  function getDistance() {

        if(empty(request()->header('X-location', '')))
        {
            return -1;
        }
        if($this->lng == 0 || $this->lat == 0)
        {
            return -1;
        }

        list($lng1, $lat1) = explode('-', request()->header('X-location'));
        $lng2 = $this->lng;
        $lat2 = $this->lat;
        // 将角度转为狐度
        $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
        return $s;
    }

}
